$().ready(function(){
    let page = {
        p:1,
        num:10,
        class_id:1,
        type:1,//1普通  2疾病
        start_time: '',
        end_time: '',
        foodsList:[]
    };
    function init(){
        getNextWeekday();
        getTomorrow();
        let today = getNow(1);
        datePicker('start_time','YYYY-MM-DD',today,function(dates){
           page.start_time = dates;
        });
        datePicker('end_time','YYYY-MM-DD',today,function(dates){
            page.end_time = dates;
        });
        getFoodsClass(page.type);
        //新填写菜单疾病配餐选择日期
        datePicker('f_date','YYYY-MM-DD',today,function(dates){{

        }});
        //图片上传
        imgUpload('foodsImgInput','foodsImgShow',function (res){
            // console.log(res);
            document.getElementById('foodsImgUrl').value = res;
        })
    }
    init();


    //菜品类别切换
    $('#f_type').on('change',function(){
        let type = $(this).children('option:selected').val();
        page.type = type;
        getFoodsClass(page.type);
        if(type == 2){
            $('#f_cb_box').css('display','block');
            $('#f_date_box').css('display','block');
        }
        if(type == 1){
            $('#f_cb_box').css('display','none');
            $('#f_date_box').css('display','none');
        }
    })
    //获取今日的下周日日期
    function getNextWeekday(){
        let date = new Date();
        let week = date.getDay();
        let today = dateTotimestamp(getNow(1) + ' 00:00:00');
        if(week == 0){
            week = 7;
        }
        let d_d = 15-week;
        let stamp = d_d*24*60*60-1;
        stamp = today + stamp;
        let nextSun = setTime3(stamp*1000);
        nextSun = nextSun.split('.').join('-');
        page.end_time = nextSun;
        $('#end_time').val(nextSun);

    }
    //获取明天
    function getTomorrow(){
        let today = dateTotimestamp(getNow(1) + ' 00:00:00');
        let oneDayStamp = 24*60*60;
        let tomorrowStamp = today + oneDayStamp;
        let tomorrow = setTime3(tomorrowStamp*1000);
        tomorrow = tomorrow.split('.').join('-');
        page.start_time = tomorrow;
        $('#start_time').val(tomorrow);
    }
    //时间切换
    $('.sel-btn').on('click',function(){
       let start = $('#start_time').val();
       let end = $('#end_time').val();
       page.start_time = start;
       page.end_time = end;
       getFoodsList(page.class_id,page.type,page.num,page.p,page.start_time,page.end_time);
    });
    //切换type
    $('#sel_type').on('change',function(){
        let type = $(this).children('option:selected').val();
        page.type = type;
        $('#jb-date').css('display','none');
        if(type == 2){
            $('#jb-date').css('display','flex');
        }
        getFoodsClass(page.type);
    });
    //切换class
    $('#sel_class').on('change',function(){
        let class_id = $(this).children('option:selected').val();
        page.class_id = class_id;
        getFoodsList(page.class_id,page.type,page.num,page.p,page.start_time,page.end_time);
    });
    //获取菜品分类
    function getFoodsClass(type){
        getAjax(`${global.apiUrl}/get_community_service_kitchen_class`,res=>{
            // console.log(res);
            page.class_list = res;
            page.class_id = res[0].id;
            let out_html = '';
            let in_html = `<option value="">请选择菜品类别</option>`;
            for(let i in res){
                out_html += `
                    <option value="${res[i].id}">${res[i].class_name}</option>
                `
                in_html += `
                     <option value="${res[i].id}">${res[i].class_name}</option>
                `
            }
            $('#sel_class').html(out_html);
            $('#f_class').html(in_html);
            //获取菜品列表
            getFoodsList(page.class_id,page.type,page.num,page.p,page.start_time,page.end_time);
        },{
            type:type,
            kitchen_id: global.kitchen_id
        })
    };
    //获取菜品列表
    function getFoodsList(class_id,type,num,p,start,end){
        let start_time = '';
        let end_time = '';
        if(start&&end){
            start_time = start;
            end_time = end;
        }
        $('#pt').css('display','none');
        $('#jb').css('display','none');
        getAjax(`${global.apiUrl}/get_community_service_kitchen_product`,res=>{
            // console.log(res);
            let list = res.list;
            page.foodsList = list;
            let html = '';
            if(type == 1){
                $('#jb').css('display','none');
                $('#pt').css('display','block');
                if(list.length==0){
                    $('#page').paging({
                        pageNo:0,
                        totalPage:0,
                        totalSize:0
                    });
                    html += `<tr>
                                <td colspan="7" style="color:#aaa;text-align:center;">无菜品</td>
                            </tr>`;
                }else{
                    $('#page').paging({
                        pageNo:p,
                        totalPage:Math.ceil(res.count/num),
                        totalSize:res.count,
                        callback(pages){
                            page.p = pages;
                            getFoodsList(class_id,type,num,pages,start,end)
                        }
                    });
                    for(let i in list){
                        html += `
                        <tr>
                            <td>
                             <img src="${list[i].head_img}" alt="" style="width: 50px">
                            </td>
                            <td>${list[i].product_name}</td>
                            <td>${list[i].price}</td>
                            <td style="width: 200px;">${list[i].function}</td>
                            <td>${list[i].suitable}</td>
                            <td>${list[i].food}</td>
                            <td class="cz">
                                <span data-target="#foodsModal" data-toggle="modal" data-id="${list[i].id}" data-type="2">修改</span>
                                <span style="margin-left: 10px" data-id="${list[i].id}" class="del">删除</span>
                            </td>
                        </tr>
                    `
                    }
                }

                $('#order_list').html(html);
            }
            if(type == 2){
                $('#pt').css('display','none');
                $('#jb').css('display','block');
                if(list.length==0){
                    $('#page0').paging({
                        pageNo:0,
                        totalPage:0,
                        totalSize:0
                    })
                    html += `<tr>
                                <td colspan="8" style="text-align: center;color:#aaa;">无菜品</td>
                            </tr>`;
                }else{
                    $('#page0').paging({
                        pageNo:p,
                        totalPage:Math.ceil(res.count/num),
                        totalSize:res.count,
                        callback(pages){
                            page.p = pages;
                            getFoodsList(class_id,type,num,pages,start,end)
                        }
                    })
                    for(let i in list){
                        html += `
                        <tr>
                            <td>
                                <img src="${list[i].head_img}" alt="" style="width: 50px">
                            </td>
                            <td>${list[i].product_name}</td>
                            <td>${list[i].price}</td>
                            <td style="width: 200px;">${list[i].function}</td>
                            <td>${list[i].suitable}</td>
                            <td>${list[i].food}</td>
                            <td>${list[i].date} ${list[i].status==1?'午餐':'晚餐'}</td>
                            <td class="cz">
                                <span data-target="#foodsModal" data-toggle="modal" data-id="${list[i].id}" data-type="2">修改</span>
                                <span style="margin-left: 10px" data-id="${list[i].id}" class="del">删除</span>
                            </td>
                        </tr>
                    `
                    }
                }
                $('#order0_list').html(html);


            }
        },{
            class_id:class_id,
            kitchen_id:global.kitchen_id,
            type:type,
            num:num,
            p:p,
            start_time:start_time,
            end_time:end_time
        })
    };
    //modal初始化
    $('#foodsModal').on('show.bs.modal',function(event){
        let modal = $(this);
        let btn = $(event.relatedTarget);
        let type = btn.data('type');
        modal.find('#foods_type').val(type);
        let modal_title = '';
        //初始化modal数据
        let f_name = modal.find('#f_name');
        f_name.val('');
        let f_price = modal.find('#f_price');
        f_price.val('');
        let f_function = modal.find('#f_function');
        f_function.val('');
        let f_suitable = modal.find('#f_suitable');
        f_suitable.val('');
        let f_food = modal.find('#f_food');
        f_food.val('');
        let f_date = modal.find('#f_date');
        f_date.val('');
        let f_type = modal.find('#f_type');
        $('#f_type').children(`option[value=${page.type}]`).prop('selected','true');
        let f_cb = modal.find('#f_cb');
        f_cb.children('option:first').prop('selected','true');
        $('#foodsImgShow').html(`<div>图片预览</div>`);
        let goodsImgUrl = modal.find('#goodsImgUrl');
        goodsImgUrl.val('');
        //不同类别 疾病配餐
        $('#f_date_box').css('display','none');
        $('#f_cb_box').css('display','none');

        let foods_type = page.type;
        if(foods_type == 2){
            $('#f_date_box').css('display','block');
            $('#f_cb_box').css('display','block');
        }
        if(type == 1){
            //添加
            modal_title = '添加菜品';

        }
        if(type == 2){
            //修改

            modal_title = '修改菜品';
            let id = btn.data('id');
            modal.find('#foods_id').val(id);
            let foodsContent = {};
            for(let i in page.foodsList){
                if(id == page.foodsList[i].id){
                    foodsContent = page.foodsList[i];
                }
            }
            if(foods_type == 2){
                $('#f_cb').children(`option[value=${foodsContent.status}]`).prop('selected','true');
            }
            $('#f_type').children(`option[value=${page.type}]`).prop('selected','true');
            $('#f_class').children(`option[value=${foodsContent.class_id}]`).prop('selected','true');
            f_name.val(foodsContent.product_name);
            f_price.val(foodsContent.price);
            f_function.val(foodsContent.function);
            f_suitable.val(foodsContent.suitable);
            f_food.val(foodsContent.food);
            f_date.val(foodsContent.date);
            $('#foodsImgUrl').val(foodsContent.head_img);
            $('#foodsImgShow').html(`<img src='${foodsContent.head_img}' />`);
        }
        modal.find('#foodsModalLabel').text(modal_title);
    });
    //点击提交事件
    $('#submit_foods_btn').on('click',function(){
        let id = $('#foods_id').val();
        let product_name = $('#f_name').val();
        let head_img = $('#foodsImgUrl').val();
        let price = $('#f_price').val();
        let class_id = $('#f_class').children('option:selected').val();
        let kitchen_id = global.kitchen_id;
        let f_type = $('#f_type').children('option:selected').val();//厨房类别
        let type = $('#foods_type').val();
        let food = $('#f_food').val();
        let f_function = $('#f_function').val();
        let suitable = $('#f_suitable').val();
        let status = '';
        let date = '';
        if(f_type == 2){
            status = $('#f_cb').children('option:selected').val();
            date = $('#f_date').val();
            if(!status){
                alert('请选择菜品餐别！');
                return;
            }else if(!date){
                alert('请选择菜单日期！');
                return;
            }
        }
        let pro = {
            id:id,
            product_name:product_name,
            head_img:head_img,
            price:price,
            class_id:class_id,
            kitchen_id:kitchen_id,
            date:date,
            status:status,
            food:food,
            function:f_function,
            suitable:suitable
        };
        pro = JSON.stringify(pro);
        getAjax(`${global.apiUrl}/operation_community_service_kitchen_product`,res=>{
            console.log(res);
            if(res == 1){
                alert('提交成功');
                $('#foodsModal').modal('hide');
                getFoodsList(page.class_id,page.type,page.num,page.p,page.start_time,page.end_time);
            }
        },{
            type:type,
            pro:pro
        })
    });
    //删除
    $('.kitchen-box').on('click','.del',function(){
        let id = $(this).data('id');
        console.log(id);
        let fr = confirm('是否确认删除该菜品？');
        if(fr){
            getAjax(`${global.apiUrl}/operation_community_service_kitchen_product`,res=>{
                if(res==1){
                    alert('删除成功！');
                    getFoodsList(page.class_id,page.type,page.num,page.p,page.start_time,page.end_time);
                }
                clear
            },{
                type:3,
                id:id
            })
        }
    })
})
